package com.hongdee.scm.estimatedprice.service.impl;

import com.hongdee.common.util.TokenUtil;
import com.hongdee.scm.estimatedprice.dao.ITScmEstimatedPriceDao;
import com.hongdee.scm.estimatedprice.entity.TScmEstimatedPrice;
import com.hongdee.scm.estimatedprice.service.ITScmEstimatedPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmEstimatedPriceServiceImpl
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.estimatedprice.service.impl
 * 作  者: 术洪超
 * 日  期:2017-06-05 19:27
 */
@Service
public class TScmEstimatedPriceServiceImpl implements ITScmEstimatedPriceService {

    String tabName = "T_SCM_EstimatedPrice";

    @Autowired
    private ITScmEstimatedPriceDao itScmEstimatedPriceDao;

    @Override
    public Map info(String id) throws Exception {
        List params = new ArrayList();
        String sql = "select * from " + tabName + " where id = ?";
        params.add(id);
        return this.itScmEstimatedPriceDao.info(sql, params.toArray());
    }

    @Override
    public TScmEstimatedPrice save(TScmEstimatedPrice data, String uid) throws Exception {

        List list = this.itScmEstimatedPriceDao.list("select * from t_scm_estimatedprice where configure_code =? and company_id = ? ",data.getConfigureCode(),TokenUtil.getCompanyId(uid));
        if(list.size()>0){
            Map m = (Map) list.get(0);
            data.setId(m.get("id").toString());
        }
        if (data.getId() == null) {
            data.setCreateTime(Calendar.getInstance().getTimeInMillis());
            data.setCreateUser(uid);
        } else {
            data.setUpdateTime(Calendar.getInstance().getTimeInMillis());
            data.setUpdateUser(uid);
        }
        data.setCompanyId(TokenUtil.getCompanyId(uid));
        this.itScmEstimatedPriceDao.saveOrUpdate(data);
        return data;
    }

    @Override
    public Map page(String start, String limit, String configureCode, String uid,String carModelId) throws Exception {

        List params = new ArrayList();
        StringBuffer sql = new StringBuffer();

        if (!StringUtils.isEmpty(configureCode)) {
            sql.append("select e.id,e.company_id,e.configure_code configure_id, e.price,c.configure_code " +
                    " from T_SCM_EstimatedPrice e left join t_bc_carconfigure c on e.configure_code = c.id  where company_id = ?");
            params.add(TokenUtil.getCompanyId(uid));
            sql.append(" and e.configure_code = ? ");
            params.add(configureCode);
        }
        if(!StringUtils.isEmpty(carModelId)){
           sql.append(" select t.id,t.configure_name,e.price,t.configure_code from (select c.id,c.configure_name,c.configure_code from t_bc_carconfigure c " +
                   "where c.model_id = ? ) t left join ( select * from t_scm_estimatedprice where company_id = ? ) e " +
                   "on t.id = e.configure_code");
           params.add(carModelId);
           params.add(TokenUtil.getCompanyId(uid));
        }

        return this.itScmEstimatedPriceDao.page(sql.toString(), params.toArray(), start, limit);
    }
}
